package com.ng.application.activity.general;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.ng.application.R;
import com.ng.application.activity.MainActivity;
import com.ng.application.adapter.GuideAdapter;
import com.ng.application.config.Constant;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Ng
 * @Date: 2024/4/9
 * @Versopn: 1.0.0
 * @Description:
 */
public class GuideActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private List<ImageView> imageViews;
    private int[] imgs = {R.mipmap.guide, R.mipmap.guide, R.mipmap.guide, R.mipmap.guide};
    private Button button;
    private ImageView[] dotViews;
    private GuideAdapter guideAdapter;

    @Override
    protected void onCreate(@NonNull Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);

        findView();
    }

    private void findView() {
        viewPager = findViewById(R.id.guide_viewPager);
        button = findViewById(R.id.guide_button);

        // 初始化图片
        initImgs();
        // 初始化底部原点指示器
        initDots();

        guideAdapter = new GuideAdapter(imageViews);
        viewPager.setAdapter(guideAdapter);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor editor = getSharedPreferences(Constant.APPLICATION_DATA, MODE_PRIVATE).edit();
                editor.putString(Constant.IS_FIRST, "1");
                editor.apply();
                Intent intent = new Intent(GuideActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int index = 0; index < dotViews.length; index++) {
                    if (position == index) {
                        dotViews[index].setImageResource(R.drawable.dot_select);
                    } else {
                        dotViews[index].setImageResource(R.drawable.dot_normal);
                    }
                    if (position == dotViews.length - 1) {
                        button.setVisibility(View.VISIBLE);
                    } else {
                        button.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 初始化图片
     */
    private void initImgs() {
        ViewPager.LayoutParams params = new ViewPager.LayoutParams();
        imageViews = new ArrayList<ImageView>();
        for (int index = 0; index < imgs.length; index++) {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(params);
            imageView.setImageResource(imgs[index]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageViews.add(imageView);
        }
    }

    /**
     * 初始化圆点指示器
     */
    private void initDots() {
        LinearLayout linearLayout = findViewById(R.id.guide_linearLayout);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);
        params.setMargins(10, 0, 10, 0);
        dotViews = new ImageView[imgs.length];
        for (int index = 0; index < imageViews.size(); index++) {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(params);
            imageView.setImageResource(R.drawable.dot_normal);
            if (index == 0) {
                imageView.setImageResource(R.drawable.dot_select);
            } else {
                imageView.setImageResource(R.drawable.dot_normal);
            }
            dotViews[index] = imageView;
            final int row = index;
            dotViews[row].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    viewPager.setCurrentItem(row);
                }
            });
            linearLayout.addView(imageView);
        }
    }
}
